<script setup lang="ts">
import {computed} from 'vue'
import {convertToMenuOption,} from "./MenuUtils"

defineOptions({
  name: 'Menu'
})

interface Props {
  collapsed: boolean
  menuItems: Menu.MenuModel[]
  activeKey: string | null
}

const props = defineProps<Props>()
const emit = defineEmits(['select'])

const menuOptions = computed(() => props.menuItems.map(convertToMenuOption))

function init() {
  console.log('menuOptions', menuOptions.value)
}

function handleUpdateValue(key: string) {
  emit('select', key)
}

init()
</script>

<template>
  <div class="menu-container">
    <div class="menu-header">
      <div class="logo-container">
        <img src="/static/img/favicon.svg" alt="logo" class="logo-img"/>
      </div>
      <h2 v-show="!props.collapsed" class="menu-title">管理系统</h2>
    </div>
    <n-menu
      class="menu-content"
      :collapsed="props.collapsed"
      :collapsed-width="64"
      :collapsed-icon-size="22"
      :options="menuOptions"
      :value="props.activeKey"
      @update:value="handleUpdateValue"
    />
  </div>
</template>

<style scoped>
.menu-container {
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
  background-color: #ffffff;
}

.menu-header {
  height: 64px;
  padding: 0 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  border-bottom: 1px solid rgba(220, 214, 214, 0.09);
  overflow: hidden;
  flex-shrink: 0;
  transition: all 0.3s ease;
}

.logo-container {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  background: linear-gradient(135deg, #1890ff 0%, #ffffff 100%);
  flex-shrink: 0;
}

.logo-img {
  width: 24px;
  height: 24px;
  filter: brightness(0) invert(1);
}

.menu-title {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #000000;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: opacity 0.3s ease;
}

.menu-content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  border-right: 1px solid rgba(197, 181, 181, 0.09);
}

/* 自定义滚动条 */
.menu-content::-webkit-scrollbar {
  width: 6px;
}

.menu-content::-webkit-scrollbar-track {
  background: transparent;
}

.menu-content::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 3px;
}

.menu-content::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.3);
}

:deep(.n-menu) {
  background-color: #fcfcfc;
}

:deep(.n-menu-item) {
  height: 50px;
  color: rgba(255, 255, 255, 0.65);
}

:deep(.n-menu-item:hover) {
  color: #fff;
}

:deep(.n-menu-item-content__icon) {
  color: rgba(255, 255, 255, 0.65);
}

:deep(.n-menu-item--selected) {
  color: #fff;
  background-color: #ffffff;
}

:deep(.n-menu-item--selected .n-menu-item-content__icon) {
  color: #fff;
}

:deep(.n-submenu) {
  color: rgba(255, 255, 255, 0.65);
}

:deep(.n-submenu-children) {
  background-color: #ffffff;
}
</style>
